<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="客户姓名" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入客户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!-- 客户列表表格 -->
    <el-table v-loading="loading" :data="serveList">
      <el-table-column label="客户编号" align="center" prop="id" />
      <el-table-column label="客户姓名" align="center" prop="nickname" />
      <el-table-column label="手机号码" align="center" prop="phone" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.createTime, "{y}-{m}-{d} {h}:{m}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === 0 ? '' : 'info'">
            {{ scope.row.status === 0 ? "正常" : "冻结" }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="冻结原因"
        align="center"
        prop="accountLockReason"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleToggleStatus(scope.row)"
            v-hasPermi="['customer:serve:edit']"
          >
            {{ scope.row.status === 0 ? "冻结" : "解冻" }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 弹出层 -->
    <el-dialog :visible.sync="dialogVisible" title="冻结原因" width="30%">
      <el-form :model="form">
        <el-form-item label="冻结原因" :label-width="formLabelWidth">
          <el-input
            type="textarea"
            v-model="form.accountLockReason"
            placeholder="请输入冻结原因"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitStatusChange">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listServe, getServe, updateServe } from "@/api/customer/serve";

export default {
  name: "Serve",
  data() {
    return {
      loading: true,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      serveList: [],
      title: "",
      open: false,
      dialogVisible: false,
      currentRow: null,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickname: null,
        phone: null,
      },
      form: {
        accountLockReason: "",
      },
      rules: {},
      formLabelWidth: "120px",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      listServe(this.queryParams).then((response) => {
        this.serveList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleToggleStatus(row) {
      this.currentRow = row;
      if (row.status === 0) {
        this.form.accountLockReason = "";
        this.dialogVisible = true;
      } else {
        this.$confirm("确认要解冻吗?", "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.updateStatus(row.id, 0, "");
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消解冻",
            });
          });
      }
    },
    submitStatusChange() {
      if (this.currentRow.status === 0) {
        this.updateStatus(this.currentRow.id, 1, this.form.accountLockReason);
      } else {
        this.updateStatus(this.currentRow.id, 0, "");
      }
    },
    updateStatus(id, status, reason = "") {
      updateServe({ id, status, accountLockReason: reason })
        .then(() => {
          this.$message({
            type: "success",
            message: "状态更新成功",
          });
          this.dialogVisible = false;
          this.getList(); // 重新加载列表
        })
        .catch(() => {
          this.$message({
            type: "error",
            message: "状态更新失败",
          });
        });
    },
  },
};
</script>

<style>
.app-container {
  padding: 20px;
}

.el-tag {
  margin-right: 10px;
}

.el-button--mini {
  margin-right: 5px;
}

.dialog-footer {
  text-align: right;
}
</style> 